﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logistics_Service.DTO;
using System.Data;

namespace Logistics_Service.DAO
{
    public class LoaiVanChuyenDAO
    {
        public LoaiVanChuyenDTO[] DanhSachLoaiVanChuyen()
        {
            LoaiVanChuyenDTO[] arr;
            string query = string.Format("select * from LoaiVanChuyen");
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            int n = dt.Rows.Count;
            arr = new LoaiVanChuyenDTO[n];
            for (int i = 0; i < n; i++)
            {
                object kh = GetDataFromDataRow(dt, i);
                arr[i] = (LoaiVanChuyenDTO)kh;
            }
            return arr;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            LoaiVanChuyenDTO lvc = new LoaiVanChuyenDTO();
            lvc.Ma = dt.Rows[i]["Ma"].ToString();
            lvc.Ten = dt.Rows[i]["Ten"].ToString();
            lvc.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
            return (object)lvc;
        }

        public bool ThemLoaiVanChuyen(string ma, string ten)
        {
            string query = string.Format("insert into loaivanchuyen(Ma,Ten) values (N'{0}',N'{1}')", ma, ten);
            DataProvider provider = new DataProvider();
            return provider.ExecuteNonQuery(query);
        }
        public bool XoaLoaiVanChuyen(string malvc)
        {
            string query = string.Format("update loaivanchuyen set daxoa = true where ma=N'{0}'", malvc);
            DataProvider provider = new DataProvider();
            return provider.ExecuteNonQuery(query);

        }
        public bool CapNhatLoaiVanChuyen(string malvc, string tenlvc)
        {
            string query = string.Format("update loaivanchuyen set ten = N'{0}' where ma = '{1}'", malvc, tenlvc);
            DataProvider provider = new DataProvider();
            return provider.ExecuteNonQuery(query);
        }
    }
}